package com.farm.kaiping.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.farm.common.security.SecurityUtils;
import com.farm.kaiping.entity.SysTenant;
import com.farm.kaiping.po.SysTenantPO;
import com.farm.kaiping.mapper.SysTenantMapper;
import com.farm.kaiping.service.SysTenantService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farm.common.util.StringRange;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 租户表(SysTenant)表服务实现类
 *
 * @author zhuhui
 * @since 2024-12-01 16:14:33
 */
@Service
@Slf4j
@AllArgsConstructor
public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant> implements SysTenantService {
    
	private LambdaQueryWrapper<SysTenant> generateQueryWrapper(SysTenantPO sysTenantPO){
		LambdaQueryWrapper<SysTenant> queryWrapper = new LambdaQueryWrapper<>();
		// 租户id
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getId()), SysTenant::getId, sysTenantPO.getId());
		queryWrapper.in(ObjectUtil.isNotEmpty(sysTenantPO.getIdList()), SysTenant::getId, sysTenantPO.getIdList());
		// 名称 唯一
		queryWrapper.like(ObjectUtil.isNotEmpty(sysTenantPO.getName()), SysTenant::getName, sysTenantPO.getName());
		// 编号
		queryWrapper.like(ObjectUtil.isNotEmpty(sysTenantPO.getCode()), SysTenant::getCode, sysTenantPO.getCode());
		// 租赁开始时间
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getStartTime()), SysTenant::getStartTime, sysTenantPO.getStartTime());
		// 租赁结束时间
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getEndTime()), SysTenant::getEndTime, sysTenantPO.getEndTime());
		// 状态：0正常 1停用 2 过期
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getAccountStatus()), SysTenant::getAccountStatus, sysTenantPO.getAccountStatus());
		// 租户logo
		queryWrapper.like(ObjectUtil.isNotEmpty(sysTenantPO.getLogo()), SysTenant::getLogo, sysTenantPO.getLogo());
		// 租户等级
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getLevelId()), SysTenant::getLevelId, sysTenantPO.getLevelId());
		// 域名
		queryWrapper.like(ObjectUtil.isNotEmpty(sysTenantPO.getDomainName()), SysTenant::getDomainName, sysTenantPO.getDomainName());
		// 省
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getProvince()), SysTenant::getProvince, sysTenantPO.getProvince());
		// 市
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getCity()), SysTenant::getCity, sysTenantPO.getCity());
		// 区
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getArea()), SysTenant::getArea, sysTenantPO.getArea());
		// 街道
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getStreet()), SysTenant::getStreet, sysTenantPO.getStreet());
		// 社区
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getCommunity()), SysTenant::getCommunity, sysTenantPO.getCommunity());
		// 详细地址
		queryWrapper.like(ObjectUtil.isNotEmpty(sysTenantPO.getAddress()), SysTenant::getAddress, sysTenantPO.getAddress());
		// 纬度
		queryWrapper.like(ObjectUtil.isNotEmpty(sysTenantPO.getLat()), SysTenant::getLat, sysTenantPO.getLat());
		// 经度
		queryWrapper.like(ObjectUtil.isNotEmpty(sysTenantPO.getLon()), SysTenant::getLon, sysTenantPO.getLon());
		// 联系人
		queryWrapper.like(ObjectUtil.isNotEmpty(sysTenantPO.getLinkman()), SysTenant::getLinkman, sysTenantPO.getLinkman());
		// 联系方式
		queryWrapper.like(ObjectUtil.isNotEmpty(sysTenantPO.getPhone()), SysTenant::getPhone, sysTenantPO.getPhone());
		// 统一信用代码
		queryWrapper.like(ObjectUtil.isNotEmpty(sysTenantPO.getCreditCode()), SysTenant::getCreditCode, sysTenantPO.getCreditCode());
		// 账号类型：0有过期限制 1 永久有效
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getAccountType()), SysTenant::getAccountType, sysTenantPO.getAccountType());
		// 剩余天数
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getRemainDay()), SysTenant::getRemainDay, sysTenantPO.getRemainDay());
		// 证明材料，可多张，逗号分隔
		queryWrapper.like(ObjectUtil.isNotEmpty(sysTenantPO.getReferenceInfo()), SysTenant::getReferenceInfo, sysTenantPO.getReferenceInfo());
		// 续约次数 ，默认0
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getRenewNum()), SysTenant::getRenewNum, sysTenantPO.getRenewNum());
		// 续约时间：可多次，逗号分隔
		queryWrapper.like(ObjectUtil.isNotEmpty(sysTenantPO.getRenewTime()), SysTenant::getRenewTime, sysTenantPO.getRenewTime());
		// 状态：0正常 1停用 2 过期
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getStatus()), SysTenant::getStatus, sysTenantPO.getStatus());
		// 软删除标志：0正常 1删除
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getDelFlag()), SysTenant::getDelFlag, sysTenantPO.getDelFlag());
        queryWrapper.eq(ObjectUtil.isEmpty(sysTenantPO.getDelFlag()), SysTenant::getDelFlag, 0);
		// 新增时间
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getCreateTime()), SysTenant::getCreateTime, sysTenantPO.getCreateTime());
		// 创建人ID
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getCreateUserId()), SysTenant::getCreateUserId, sysTenantPO.getCreateUserId());
		// 创建人姓名
		queryWrapper.like(ObjectUtil.isNotEmpty(sysTenantPO.getCreateUserName()), SysTenant::getCreateUserName, sysTenantPO.getCreateUserName());
		// 更新时间
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getUpdateTime()), SysTenant::getUpdateTime, sysTenantPO.getUpdateTime());
		// 更新人ID
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getUpdateUserId()), SysTenant::getUpdateUserId, sysTenantPO.getUpdateUserId());
		// 更新人姓名
		queryWrapper.like(ObjectUtil.isNotEmpty(sysTenantPO.getUpdateUserName()), SysTenant::getUpdateUserName, sysTenantPO.getUpdateUserName());
		// 租户ID
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysTenantPO.getTenantId()), SysTenant::getTenantId, sysTenantPO.getTenantId());
		// 创建时间时间区间查询
		queryWrapper.ge(ObjectUtil.isNotEmpty(StringRange.getOne(sysTenantPO.getCreateTimeBetween(),0)), SysTenant::getCreateTime,StringRange.getOne(sysTenantPO.getCreateTimeBetween(),0));
		queryWrapper.le(ObjectUtil.isNotEmpty(StringRange.getOne(sysTenantPO.getCreateTimeBetween(),1)), SysTenant::getCreateTime,StringRange.getOne(sysTenantPO.getCreateTimeBetween(),1));
		// 更新时间时间区间查询
		queryWrapper.ge(ObjectUtil.isNotEmpty(StringRange.getOne(sysTenantPO.getUpdateTimeBetween(),0)), SysTenant::getUpdateTime,StringRange.getOne(sysTenantPO.getUpdateTimeBetween(),0));
		queryWrapper.le(ObjectUtil.isNotEmpty(StringRange.getOne(sysTenantPO.getUpdateTimeBetween(),1)), SysTenant::getUpdateTime,StringRange.getOne(sysTenantPO.getUpdateTimeBetween(),1));

		queryWrapper.orderByDesc(SysTenant::getCreateTime,SysTenant::getId);
		return queryWrapper;
	}

	 /**
	  * @describe 分页查询租户表
	  * @author zhuhui
	  * @date 2024-12-01 16:14:33
	  */
	@Override
	public Page<SysTenant> getSysTenantPage(Page page, SysTenantPO sysTenantPO){
		LambdaQueryWrapper<SysTenant> queryWrapper = this.generateQueryWrapper(sysTenantPO);
        return this.baseMapper.selectPage(page, queryWrapper);
	}
	
	/**
     * @describe 查询租户表list
     * @author  zhuhui
     * @date  2024-12-01 16:14:33
     */
	@Override
	public List<SysTenant> getSysTenantList(SysTenantPO sysTenantPO){
		LambdaQueryWrapper<SysTenant> queryWrapper = this.generateQueryWrapper(sysTenantPO);
        return this.baseMapper.selectList(queryWrapper);
	}
	
	/**
     * @describe 获取一条数据 租户表
     * @author  zhuhui
     * @date  2024-12-01 16:14:33
     */
	@Override
	public SysTenant getSysTenant(SysTenantPO sysTenantPO){
		LambdaQueryWrapper<SysTenant> queryWrapper = this.generateQueryWrapper(sysTenantPO);
        queryWrapper.last("LIMIT 1");
        return this.baseMapper.selectOne(queryWrapper);
	}

    /**
     * @describe 新增租户表
     * @author  zhuhui
     * @date  2024-12-01 16:14:33
     */
	@Override
	public Integer saveSysTenant(SysTenantPO sysTenantPO) {
        sysTenantPO.setDelFlag(0);
		sysTenantPO.setTenantId(SecurityUtils.getUser().getTenantId());// 租户ID
		sysTenantPO.setCreateTime(LocalDateTime.now());
		sysTenantPO.setCreateUserId(SecurityUtils.getUser().getUserId());// 创建人
		sysTenantPO.setCreateUserName(SecurityUtils.getUser().getRealName());// 创建人
		sysTenantPO.setUpdateTime(LocalDateTime.now());
		SysTenant sysTenant = BeanUtil.copyProperties(sysTenantPO, SysTenant.class);
		this.save(sysTenant);
		return sysTenant.getId();
	}

	/**
     * @describe 修改租户表
     * @author  zhuhui
     * @date  2024-12-01 16:14:33
     */
	@Override
	public String updateSysTenant(SysTenantPO sysTenantPO) {
		if (ObjectUtil.isEmpty(sysTenantPO.getId())) {
			throw new RuntimeException("id为空");
		}

		sysTenantPO.setUpdateUserId(SecurityUtils.getUser().getUserId());// 更新人
		sysTenantPO.setUpdateUserName(SecurityUtils.getUser().getRealName());// 更新人
		sysTenantPO.setUpdateTime(LocalDateTime.now());
		SysTenant sysTenant = BeanUtil.copyProperties(sysTenantPO, SysTenant.class);
		this.updateById(sysTenant);
		return "修改成功";
	}

	/**
     * @describe 通过id删除租户表
     * @author  zhuhui
     * @date  2024-12-01 16:14:33
     */
	@Override
	public String removeSysTenant(SysTenantPO sysTenantPO) {
		if (ObjectUtil.isEmpty(sysTenantPO.getBatchDel())) {
			throw new RuntimeException("删除数据未选择");
		}
		LambdaUpdateWrapper<SysTenant> removeWrapper = new LambdaUpdateWrapper<>();
		removeWrapper.in(SysTenant::getId, sysTenantPO.getBatchDel());
		removeWrapper.set(SysTenant::getDelFlag,1);
		removeWrapper.set(SysTenant::getUpdateUserId,SecurityUtils.getUser().getUserId());// 更新人
		removeWrapper.set(SysTenant::getUpdateUserName,SecurityUtils.getUser().getRealName());// 更新人
		removeWrapper.set(SysTenant::getUpdateTime,LocalDateTime.now());

		this.update(removeWrapper);
		return "删除成功";
	}
	
}

